package com.ruoyi.task;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.quartz.domain.SysJob;
import com.ruoyi.quartz.service.ISysJobService;
import com.ruoyi.service.OrderRegisterService;
import com.ruoyi.utils.DingTalkUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;

@Component("baoShangTask")
public class BaoShangTask {

    @Autowired
    OrderRegisterService orderRegisterService;

    @Autowired
    ISysJobService jobService;

    public void getBaoShangOrderData() {
        try{
            String baoShangToken = orderRegisterService.getBaoShangToken();
            Map<String, String> map = new HashMap();
            map.put("Authorization", baoShangToken);
            String rspStr = HttpUtils.sendGet("http://api.baoshanglvyou.com/api/order", "page=0&size=50&types=N&statues=2&sort=orderTime,desc", Constants.UTF8, map);
            JSONObject jsonObject = JSONObject.parseObject(rspStr);
            if(jsonObject == null){
                // 发消息并关闭定时任务
                SysJob sysJob = new SysJob();
                sysJob.setJobId(5L);
                sysJob.setStatus("1");
                jobService.changeStatus(sysJob);
                String accessToken = DingTalkUtils.getAccessToken();
                DingTalkUtils.sendMessage(accessToken,"21501764271176528");
                return;
            }
            JSONArray jsonArray = jsonObject.getJSONArray("content");
            if (jsonArray.size() > 0) {
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject orderObj = jsonArray.getJSONObject(i);
                    String bsHotelId = orderObj.getString("localHotelId");
                    String handleTime = DateUtils.getDateByTimestamp(orderObj.getLong("confirmTime"));
                    String orderCode = orderObj.getString("id");
                    String eOrderCode = orderObj.getString("canalsOrderId");
                    String names = orderObj.getString("customerNames");
                    String cgj = orderObj.getString("settleTotal");
                    String jsj = orderObj.getString("total");
                    String startTime = DateUtils.getDateByTimestamp(orderObj.getLong("inTime"));
                    String endTime = DateUtils.getDateByTimestamp(orderObj.getLong("outTime"));
                    int fjsNum = orderObj.getInteger("customerNum");

                    JSONObject jsonObject1 = new JSONObject();
                    jsonObject1.put("bsHotelId", bsHotelId);
                    jsonObject1.put("handleTime", handleTime);
                    jsonObject1.put("orderCode", orderCode);
                    jsonObject1.put("eOrderCode", eOrderCode);
                    jsonObject1.put("names", names);
                    jsonObject1.put("cgj", cgj);
                    jsonObject1.put("jsj", jsj);
                    jsonObject1.put("startTime", startTime);
                    jsonObject1.put("endTime", endTime);
                    jsonObject1.put("fjsNum", fjsNum);
                    jsonObject1.put("platformName", "宝尚");
                    orderRegisterService.importBaoShangOrder(jsonObject1);
                }
            }
        } catch (Exception e){

        }
    }
}